﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MO.ModelingClient
{
    public partial class FieldForm : Form
    {
        Dictionary<string, string> refDict = new Dictionary<string, string>();
        public FieldForm()
        {
            InitializeComponent();
        }

        private void FieldForm_Shown(object sender, EventArgs e)
        {
            TreeNode sysNode = treeView.Nodes.Add("系统");
            refDict[""] = "";
            DataTable table = DbOperation.DB.ExecuteDataTable("select ED_Code,ED_Name from Entity_Define"); //MySqlHelper.GetDataTable("select * from "+config.TableName);
            foreach (DataRow row in table.Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = row["ED_Name"].ToString();
                node.Tag = row["ED_Code"];
                refDict[node.Tag.ToString()] = node.Text;
                if (IsSysNode(node.Tag))
                    sysNode.Nodes.Add(node);
                else
                    treeView.Nodes.Add(node);
            }
        }
        bool IsSysNode(object code)
        {
            string c = code.ToString();
            if (c == "ORGAN" || c == "USER" || c == "ROLE")
                return true;
            return false;
        }
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null)
            {
                tableControl1.Enabled = false;
                return;
            }
            tableControl1.Enabled = true;
            DataConfig config = new DataConfig();
            config.TableName = "Field_Define";
            config.KeyField = "FD_Code";
            config.TitleField = "FD_Name";
            config.OrderField = "FD_Order";
            config.KeyDict["ED_Code"] = e.Node.Tag;
            config.AddItem(new ItemData("FD_Code", "编码") { Default = e.Node.Tag + "_" });
            config.AddItem(new ItemData("FD_Name", "名称"), true);
            config.AddItem(new ItemData("FD_Description", "描述"));
            config.AddItem(new ItemData("FD_Order", "排序")
            {
                Type = DataType.Number,
                CreateControlDelegate = delegate(ItemBaseControl control)
                {
                    control.SetValue(config.TableControl.GetCount() * 2);
                }
            });
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict[FieldType.String] = "字符串";
            dict[FieldType.Number] = "数字";
            dict[FieldType.Time] = "时间";
            //dict[FieldType.Blob] = "二进制";
            config.AddItem(new ItemData("FD_Type", "指标类型") { Type = DataType.Enum, Enums = dict });
            config.AddItem(new ItemData("FD_Length", "长度") { Type = DataType.Number });
            config.AddItem(new ItemData("FD_NotNull", "不能为空") { Type = DataType.Bool });
            config.AddItem(new ItemData("FD_Reference", "引用指标") { Type = DataType.Enum, Enums = refDict });
            tableControl1.Init(config);
        }
    }
}
